<template>
	<view class="page">
		<u-navbar title-color="#333" title-bold title-size="36" title="AI大模型" :border-bottom="false"></u-navbar>
		<!-- <hx-navbar ref="hxnb" :config="config">
			<block slot="max">
				<view class="nav_wrap flex">
					<view class="back_wrap flex" @click="back">
						<u-icon name="arrow-left" color="#333" size="36"></u-icon>
					</view>
					<view class="page_title font_18_333">AI大模型</view>
				</view>
			</block>
		</hx-navbar> -->
		<!-- <view class="main-div">
			<view class="zhanwei" :style="{'height':statusBarHeight+'rpx'}"></view>
			<view class="flex flex_end topicon">
				<image class="ai-icon" src="../../static/img/aiadd.png" mode="" @click="addMess"></image>
				<image class="ai-icon u-m-l-56" src="../../static/img/ailist.png" mode="" @click="getMess"></image>
			</view>
		</view> -->
		<view class="flex flex_end topicon">
			<image class="ai-icon" src="../../static/img/aiadd.png" mode="" @click="addMess"></image>
			<image class="ai-icon u-m-l-56" src="../../static/img/ailist.png" mode="" @click="getMess"></image>
		</view>
		<view class="topicon-zw"></view>
		<image class="aibg" src="../../static/img/ai-bg.png" mode=""></image>
		<scroll-view :scroll-top="scrollTop" scroll-y="true" :scroll-into-view="'sssdddfff'">
			<view class="itemms">
				<view v-for="(item,idx) in messgageChatlist" :key="idx" :id="item.id">
					<view :class="item.type==1?'chatself':'chatother'">
						<image v-if="item.type==1" mode="aspectFill" :src="userInfo.image" class="headimg u-m-l-20">
						</image>
						<image v-else src="../../static/img/aihead.png" class="headimg u-m-r-20" mode="aspectFill">
						</image>
						<view class="font_16_333">
							<!-- <view class="font_12_666 u-m-t-12"
								:style="{'text-align': item.type==1?'right':'left'}">
								{{item.type==1?'自己':'问理律助手'}}
							</view> -->
							<view :class="item.type==1?'chatbgvS':'chatbgvO'">
								<!-- 正常文字或链接地址 -->
								<view class="">
									{{item.content}}
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="" id="sssdddfff">
				
			</view>
		</scroll-view>
		<view class="zhanwei"></view>
		<view class="chatinput" id="meau">
			<view class="flex shurukuang">
				<u-input placeholder="请在此输入内容" :clearable="false" v-model="message"  class="inputtext u-m-l-20"  @confirm="sendMessage()" />
				<!-- <u--input placeholder="输入要说的内容" border="surround" v-model="message"  @confirm="sendMessage()"></u--input> -->
				<!-- <input @confirm="sendMessage()" v-model="message" class="inputtext flex u-m-l-20" type="text"
					placeholder="输入要说的内容" /> -->
				<view class="sendbtn" @click="sendMessage()">
					<u-icon name="arrow-upward" color="#fff" size="30"></u-icon>

				</view>
			</view>
		</view>



		<u-popup v-model="poshow" mode="right" :closeable="true">
			<view class="from-text">
				<view class="qd-btn" @click="addMess">
					新建对话
				</view>
				<view class="form-title">
					历史对话
				</view>
				<view class="">
					<view class="ai-list flex flex_sb" v-for="(item,index) in duihuaList" :key="index">
						<view class="flex" @click="todetail(item)">
							<image class="aidh" src="../../static/img/aidh.png" mode=""></image>
							<view class="aiti line_1 u-m-l-20">
								{{item.title}}
							</view>
						</view>

						<image class="ai12" src="../../static/img/ai12.png" mode="" @click="del(item)"></image>
					</view>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	var statusBarHeight = (uni.getSystemInfoSync().statusBarHeight * 2);
	
	export default {
		data() {
			return {
				statusBarHeight: statusBarHeight+88,
				config: {
					maxSlot: true,
					back: false,
					barPlaceholder: false,
					backgroundColor: [0, '#FFFFFF'],
					slideBackgroundColor: [1, '#FFFFFF']
				},
				duihuaList: [],
				poshow: false,
				windowId: '',
				isFirst: true,
				userInfo: [],
				heightC: 0,
				scrollTop: 0,
				message: "", //发送内容
				messgageChatlist: [{
					content: '你好 ~ 有什么可以帮您！我可以回答您在法律方面遇到的各类问题，我的回答会司含相关参考法规和案例。回答仅供您参考，如需寻求专业意见请咨询相关专业人士。',
					type: '2'
				}],
			}
		},

		onLoad(e) {
			if(e.id){
				this.todetail(e)
				
			}
			this.getInfo()
			this.getAiDiaLogList()
			console.log(uni.getSystemInfoSync().windowHeight,'uni.getSystemInfoSync().windowHeight')
			console.log(uni.getSystemInfoSync().statusBarHeight,'uni.getSystemInfoSync().statusBarHeight')
			console.log(uni.getSystemInfoSync().screenHeight,'uni.getSystemInfoSync().screenHeight')
			// this.heightC = uni.getSystemInfoSync().windowHeight;
			// this.heightC = uni.getSystemInfoSync().windowHeight - uni.getSystemInfoSync().statusBarHeight - 160;
			this.scrollTop = 99999999
			
			
		},
		methods: {
			back() {
				uni.navigateBack()
			},
			getAiDiaLogList() {
				this.$api({
					url: '/user/getAiDiaLogList',
					method: 'post',
					data: {
						token: uni.getStorageSync("token")
					},
				}).then(res => {
					if (res.code == 1) {
						this.duihuaList = res.data.list
					}
				})
			},
			del(e) {
				let that = this
				uni.showModal({
					title: '提示',
					content: '确认删除吗',
					success: function(res) {
						if (res.confirm) {
							that.$api({
								url: '/user/deleteAiDialog',
								method: 'post',
								data: {
									token: uni.getStorageSync("token"),
									dialog_id: e.id
								},
							}).then(res => {
								if (res.code == 1) {
									that.getAiDiaLogList()
								}
							})
						}
					}
				});



			},
			todetail(e) {
				this.isFirst = false
				this.windowId = e.id
				this.$api({
					url: '/user/getAiDiaLogDetail',
					method: 'post',
					data: {
						token: uni.getStorageSync("token"),
						dialog_id: e.id
					},
				}).then(res => {
					if (res.code == 1) {
						this.poshow = false
						this.messgageChatlist = res.data
					}
				})

			},
			addMess() {
				this.poshow = false
				uni.showToast({
					title: "新建对话",
					icon: "none"
				});
				this.isFirst = true
				this.messgageChatlist = [{
					content: '你好 ~ 有什么可以帮您！我可以回答您在法律方面遇到的各类问题，我的回答会司含相关参考法规和案例。回答仅供您参考，如需寻求专业意见请咨询相关专业人士。',
					type: '2'
				}]
			},
			getMess() {
				this.poshow = true
			},
			//个人信息
			getInfo() {
				this.$api({
					url: '/getUserInfo',
					method: 'get',
					data: {
						token: uni.getStorageSync("token")
					},
				}).then(res => {
					if (res.code == 1) {
						this.userInfo = res.data
					}
				})
			},
			addAiDialog() {
				let that = this
				this.$api({
					url: '/user/addAiDialog',
					method: 'post',
					data: {
						token: uni.getStorageSync("token"),
						user_id: this.userInfo.id,
						title: this.message
					},
				}).then(res => {
					if (res.code == 1) {
						this.isFirst = false
						this.windowId = res.data
						this.getAiDiaLogList()
						this.messgageChatlist.push({
							"type": 1, //类型 1用户提问 2AI回复
							"content": this.message //问答内容
						})
						this.$nextTick(() => {
							this.scrollTop = this.scrollTop + 1000
						})
						// that.addAiDialogDetail(this.message,1)
						uni.showLoading()
						uni.request({
							method: "POST",
							url: 'http://8.222.199.105:48081/ai/chat1',
							data: {
								"channel": 1,
								"windowId": that.windowId,
								"message": that.message,
							},
							header: {
								'content-type': 'application/json'
							},
							success(res) {
								console.log(res)
								that.messgageChatlist.push({
									type: 2,
									content: res.data,
								})
								that.$nextTick(() => {
									that.scrollTop = that.scrollTop + 1000
								})
								uni.hideLoading();

								that.addAiDialogDetail(res.data, 2)
								that.message = ''
							}
						});

					}
				})
			},

			addAiDialogDetail(e, type) {
				this.$api({
					url: '/user/addAiDialogDetail',
					method: 'post',
					data: {
						token: uni.getStorageSync("token"),
						dialog_id: this.windowId,
						content: e,
						type: type
					}
				})

			},

			sendMessage() {
				let that = this
				if (this.isFirst) {
					this.addAiDialog()
					return
				}
				this.messgageChatlist.push({
					"type": 1, //类型 1用户提问 2AI回复
					"content": this.message //问答内容
				})
				this.$nextTick(() => {
					this.scrollTop = this.scrollTop + 1000
				})

				that.addAiDialogDetail(this.message, 1)

				uni.showLoading()
				uni.request({
					method: "POST",
					url: 'http://8.222.199.105:48081/ai/chat1',
					data: {
						"channel": 1,
						"windowId": that.windowId,
						"message": that.message,
					},
					header: {
						'content-type': 'application/json'
					},
					success(res) {
						console.log(res)


						that.messgageChatlist.push({
							type: 2,
							content: res.data,
						})
						that.$nextTick(() => {
							that.scrollTop = that.scrollTop + 1000
						})
						uni.hideLoading();


						that.addAiDialogDetail(res.data, 2)

						that.message = ''
					}
				});




			},
		}
	}
</script>

<style scoped lang="scss">
	.aibg {
		width: 100%;
		// height: 100vh;
		position: fixed;
		top: 0;
		left: 0;
		bottom: 0;
		right: 0;
		z-index: 0;
	}
	.main-div {
		position: relative;
		width: 100%;
	
		
	
		.zhanwei {
			width: 100%;
		}
	}
	.nav_wrap {
		width: 100%;
		position: relative;
	
		.back_wrap {
			position: absolute;
			left: 24rpx;
	
			.diqu {
				font-weight: 600;
				font-size: 32rpx;
				color: #FFFFFF;
				margin: 0 12rpx;
			}
	
			.dq-icon {
				width: 28rpx;
				height: 34rpx;
			}
		}
	
		.page_title {
			width: 100vw;
			text-align: center;
			font-weight: bold;
		}
	}
	.from-text {
		/* position: relative; */
		width: 640rpx;
		padding: 0 30rpx;
		padding-top: 220rpx;
		position: relative;
		height: 100%;

		.form-title {
			font-size: 32rpx;
			color: #222222;
			margin-top: 60rpx;
		}

		.ai-list {
			padding: 30rpx;

			.aidh {
				width: 24rpx;
				height: 20rpx;
			}

			.aiti {
				font-size: 28rpx;
				color: #333333;
				width: 380rpx;
			}

			.ai12 {
				width: 28rpx;
				height: 28rpx;
			}
		}


		.qd-btn {
			width: 576rpx;
			height: 92rpx;
			background: linear-gradient(306deg, #9055FF 0%, #00EBE2 100%);
			border-radius: 52rpx 52rpx 52rpx 52rpx;
			font-weight: 500;
			font-size: 36rpx;
			color: #FFFFFF;
			text-align: center;
			line-height: 92rpx;


		}
	}

	.topicon {
		position: fixed;
		z-index: 100;
		padding: 24rpx;
		right:32rpx;
		// top:32rpx;
	}
		.topicon-zw{
			width: 100%;
			height: 80rpx;
		}
	
	
	.ai-icon {
		width: 40rpx;
		height: 40rpx;
	}

	.page {
		position: relative;

		// .aibg {
		// 	position: absolute;
		// 	top: 0;
		// 	left: 0;
		// 	width: 100vw;
		// 	height: 100vh;
		// }
	}

	.m-m2 {
		font-size: 44rpx;
		font-weight: bold;
	}

	.zf-item {

		text-align: center;

		.zf-img {
			width: 64rpx;
			height: 80rpx;
			border-radius: 8rpx;
			margin: 0 auto;
		}
	}

	.chbox {
		width: 42rpx;
		height: 42rpx;
		background: #F81B1B;
		border-radius: 50%;
		text-align: center;
		// line-height: 24rpx;
	}

	.btn-bo {
		width: 750rpx;
		height: 120rpx;
		background: #FFFFFF;
		box-shadow: 0rpx -2rpx 8rpx 2rpx rgba(0, 0, 0, 0.14);
		padding-top: 14rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 80;

		.btn_login_btn {
			width: 686rpx;
			height: 90rpx;
			background: linear-gradient(180deg, #FA2D2D 0%, #F40000 100%);
			border-radius: 46rpx 46rpx 46rpx 46rpx;
			// margin-top: 116rpx;
			font-size: 36rpx;
			color: #FFFFFF;
			line-height: 90rpx;
			margin-left: 32rpx;
			text-align: center;

			.xiadan {
				font-size: 36rpx;
				font-weight: bold;
			}

			.m-fh {
				font-weight: bold;
				font-size: 28rpx;
			}

			.m-m {
				font-weight: bold;
				font-size: 36rpx;
			}

			.m-sj {
				font-size: 28rpx;
			}
		}
	}

	.ch-item {
		width: 686rpx;
		// height: 184rpx;
		background: #FFFFFF;
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		padding: 24rpx;
		margin: 20rpx auto;

		.jdt-div {
			position: relative;
			width: 638rpx;
			height: 76rpx;
			margin-top: 24rpx;

			.jindu {
				width: 76rpx;
				height: 76rpx;
				position: absolute;
				top: -18rpx;
				z-index: 8;
			}
		}

		.lv-item {
			width: 290rpx;
			padding: 20rpx;
			background: #FDF5EF;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			margin-right: 20rpx;
		}

		.jp-i {
			width: 24rpx;
			height: 20rpx;
		}

		.ls-xing {
			width: 24rpx;
			height: 24rpx;
		}

		.ls-fs {
			font-size: 20rpx;
			color: #FD8643;
		}

		.ls-zy {
			padding-left: 10rpx;
			border-left: 1px #999 solid;
			margin-left: 10rpx;
		}

		.lv-item-headimg {
			width: 64rpx;
			height: 80rpx;
			border-radius: 8rpx;
		}

		.wid-r {
			width: 200rpx;
		}

		.zhifubtn {
			width: 254rpx;
			height: 56rpx;
			background: linear-gradient(180deg, #FA2D2D 0%, #F40000 100%);
			border-radius: 32rpx 32rpx 32rpx 32rpx;
			margin: 0 auto;
			margin-top: 20rpx;
			color: #fff;
		}
	}

	.po-bikan {
		padding: 40rpx 32rpx;
		background: #F6F6F6;
		width: 100%;
		position: relative;

		.shuxian {
			height: 300rpx;
			width: 2rpx;
			border-left: 4rpx dashed #FA2D2D;
			position: absolute;
			left: 50rpx;
		}

		.bk-title {
			font-weight: bold;
			font-size: 32rpx;
			color: #222222;
		}

		.jine-div {
			width: 686rpx;
			// height: 504rpx;
			padding: 32rpx 24rpx;
			background: #FFFFFF;
			border-radius: 32rpx 32rpx 32rpx 32rpx;

			.jine-item {
				margin: 14rpx;
				width: 184rpx;
				height: 64rpx;
				background: #FFFFFF;
				border-radius: 32rpx 32rpx 32rpx 32rpx;
				border: 2rpx solid #E5E5E5;
				font-size: 28rpx;
				color: #000000;
				text-align: center;
				line-height: 64rpx;
			}
		}
	}

	.reBtn {
		width: 100%;
		height: 64rpx;
		background: #FFFFFF;
		border-radius: 32rpx 32rpx 32rpx 32rpx;
		border: 2rpx solid #E5E5E5;
		font-size: 28rpx;
		color: #000000;
		text-align: center;
		line-height: 64rpx;
		margin-top: 24rpx;
	}

	.colred {
		color: #FA4C2C;
	}

	.chatinput {
		position: fixed;
		bottom: 0rpx;
		z-index: 20;
		width: 100%;
		background-color: rgba(255, 255,255,0.7);
		//	display: flex;
		// justify-content: space-between;
		align-items: center;
		// border-top: 1px rgba(0, 0, 0, 0.1) solid;
		
		height: 176rpx;
		// background: #FFFFFF;
		box-shadow: 0rpx -8rpx 20rpx 2rpx rgba(0,0,0,0.06);
		border-radius: 0rpx 0rpx 0rpx 0rpx;
		

		.inputtext {
			width: 550rpx;
			color: #333333;
			font-size: 28rpx;
			// background-color: #f6f6f6;
			height: 72rpx;
			line-height: 72rpx;
			padding: 0 16rpx;
			border-radius: 222rpx;
		}

		.shurukuang {
			height: 100rpx;
			// border-bottom: 1px rgba(0, 0, 0, 0.1) solid;
			width: 700rpx;
			height: 88rpx;
			background:  rgba(255, 255,255,0.5);
			box-shadow: 0rpx 2rpx 6rpx 2rpx rgba(0,0,0,0.16);
			border-radius: 46rpx 46rpx 46rpx 46rpx;
			border: 2rpx solid #D7DAE3;
			// opacity: 0.5;
			margin: 20rpx auto;
			padding-right: 16rpx;
		}

		.sendbtn {
			// margin-left: 20rpx;
			// padding: 10rpx 20rpx;
			// background-color: #00B42A;
			// color: #FFFFFF;
			// border-radius: 8rpx;
			width: 64rpx;
			height: 64rpx;
			background: linear-gradient( 306deg, #9055FF 0%, #00EBE2 100%);
			border-radius: 52rpx 52rpx 52rpx 52rpx;
			text-align: center;
			line-height: 64rpx;
			margin-left: 20rpx;
		}
	}

	.zhanwei {
		width: 100%;
		height: 180rpx;
	}

	.itemms {
		.headimg {
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			background-color: #EEEEEE;

		}

		.chatself {
			display: flex;
			flex-direction: row-reverse;
			width: 90%;
			margin-left: 5%;
			margin-top: 32rpx;
			//margin-bottom: 40rpx;
		}

		.chatother {
			display: flex;
			width: 90%;
			margin-left: 5%;
			margin-top: 32rpx;
			margin-bottom: 40rpx;
		}

		.chatbgvS {
			margin-top: 8rpx;
			max-width: 524rpx;
			background: linear-gradient(90deg, #8CF7D7 0%, #71DAF9 100%);
			// border-radius: 0rpx 16rpx 16rpx 16rpx;

			border-radius: 8rpx;
			padding: 16rpx;
			padding-top: 12rpx;
			line-height: 44rpx;
			margin-right: -12rpx;
		}

		.chatbgvO {
			margin-top: 8rpx;
			padding: 16rpx;
			padding-top: 12rpx;
			max-width: 524rpx;
			background-color: #FFFFFF;
			border-radius: 8rpx;
			line-height: 44rpx;
			margin-left: -12rpx;
		}


	}

	.buzhou {
		width: 750rpx;
		height: 88rpx;
		background: #FFFFFF;
		padding: 0 32rpx;
		margin-bottom: 24rpx;

		.buzhou-item {

			.buzhou-item-num {
				width: 42rpx;
				height: 42rpx;
				border-radius: 50%;
				font-size: 28rpx;
				color: #FFFFFF;
				font-weight: bold;
				text-align: center;
				line-height: 42rpx;
				background: #CCCCCC;
			}


			.buzhou-item-text {
				font-size: 28rpx;
				color: #666666;
				margin-left: 12rpx;
			}

			&-sel {
				.buzhou-item-num {
					background: linear-gradient(180deg, #FA472D 0%, #F41D00 100%);
				}

				.buzhou-item-text {
					font-size: 28rpx;
					color: #333333;
					font-weight: bold;
				}
			}
		}
	}

	.ls-div {
		position: relative;
		width: 686rpx;
		// height: 120rpx;
		background: linear-gradient(90deg, #FFF8EF 0%, #FFFBF5 55%, #FFF8EF 100%);
		border-radius: 16rpx 16rpx 16rpx 16rpx;
		border: 2rpx solid #F4CF9C;
		padding: 18rpx 24rpx;
		margin: 0 auto;

		.ls-top {
			font-weight: bold;
			font-size: 32rpx;
			color: #8C5811;

			.t-num {
				color: #FD7A22;
			}
		}

		.lszy {
			font-size: 24rpx;
			color: #FF640D;
		}

		.ls-d-t {
			border-bottom: 1px #ECE4D9 dashed;
		}

		.ls-sss-item {
			margin-top: 10rpx;

			.ls-sss {
				width: 24rpx;
				height: 24rpx;
			}

			.ls-te {
				font-weight: bold;
				font-size: 20rpx;
				color: #B89D7B;
			}
		}

		.tw-lsbg {
			width: 134rpx;
			height: 136rpx;
			position: absolute;
			right: 0;
			bottom: 0;
		}

		.headimg {
			height: 84rpx;
			width: 68rpx;
			border-radius: 8rpx;
		}

		.h23 {
			width: 24rpx;
			height: 20rpx;
		}

		.h24 {
			width: 24rpx;
			height: 24rpx;
			margin-right: 8rpx;
		}

		.pf-t {
			font-size: 20rpx;
			color: #FD8643;
		}
	}
</style>